package cn.dust.design.pattern.structural.proxy.staticproxy;

import cn.dust.design.pattern.structural.proxy.IOrderService;
import cn.dust.design.pattern.structural.proxy.Order;
import cn.dust.design.pattern.structural.proxy.OrderServiceImpl;
import cn.dust.design.pattern.structural.proxy.db.DataSourceContexHolder;

/**
 * @Author: dust
 * @Date: 2019/11/27 15:11
 */
public class OrderServiceStaticProxy {
    private IOrderService iOrderService;
    public int saveOrder(Order order){

        beforeMethod(order);
        iOrderService=new OrderServiceImpl();
        int result=iOrderService.saveOrder(order);
        afterMethod();
        return result;
    }

    private void beforeMethod(Order order){
        int userId=order.getUserId();
        int dbRouter=userId%2;
        System.out.println("静态代理分配到【DB"+dbRouter+"】处理数据");

        //todo
        DataSourceContexHolder.setDBType(String.valueOf(dbRouter));
        System.out.println("静态代理 before code");
    }

    private void afterMethod(){
        System.out.println("静态代理 after code");
    }
}
